package com.appbee.article.client.ui;

import javax.inject.Inject;
import javax.inject.Provider;

import com.appbee.article.client.presenter.ArticleBoxPresenter;
import com.appbee.core.client.design.PageStyle;
import com.appbee.core.client.htmleditor.HtmlEditor;
import com.appbee.core.client.process.LoadingIndcator;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.safehtml.shared.SafeHtmlUtils;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HTML;

public class ArticleBox extends Composite implements ArticleBoxPresenter.View
{

	private boolean canEdit;
	
	private final FlowPanel flowPanel;
	private final HTML contentPanel = new HTML();
	private FlowPanel buttonBar;
	private HtmlEditor editor;
	
	private final ArticleBoxPresenter presenter;

	private final IArticleStyle articleStyle;

	private final PageStyle pageStyle;

	private final LoadingIndcator loadingIndcator;

	private final Provider<HtmlEditor> htmlEditorProvider;

	@Inject
	public ArticleBox(ArticleBoxPresenter presenter, PageStyle pageStyle,
			IArticleStyle articleStyle, LoadingIndcator loadingIndcator,
			Provider<HtmlEditor> htmlEditorProvider)
	{
		this.flowPanel = new FlowPanel();
		this.presenter = presenter;
		this.pageStyle = pageStyle;
		this.articleStyle = articleStyle;
		this.loadingIndcator = loadingIndcator;
		this.htmlEditorProvider = htmlEditorProvider;
		flowPanel.addStyleName(articleStyle.box());
		initWidget(flowPanel);
		presenter.setView(this);
	}
	
	public void setStrId(String strId)
	{
		presenter.setStrId(strId);
	}

	@Override
	public void showContent(String content)
	{
		flowPanel.clear();
		contentPanel.setHTML(SafeHtmlUtils
				.fromTrustedString(content == null ? "" : content));
		if (canEdit)
		{
			addButtonBar();
			flowPanel.removeStyleName(articleStyle.editMode());
		}
		flowPanel.add(contentPanel);
		
	}

	private void addButtonBar()
	{
		if (buttonBar == null)
		{
			createButtonBar();
		}
		flowPanel.add(buttonBar);
	}

	private void createButtonBar()
	{
		flowPanel.addStyleName(articleStyle.editBox());
		buttonBar = new FlowPanel();
		buttonBar.addStyleName(articleStyle.buttonBar());
		Button editButton = new Button("Breyta");
		editButton.setStyleName(articleStyle.editButton());
		editButton.addClickHandler(new ClickHandler()
		{

			@Override
			public void onClick(ClickEvent event)
			{
				presenter.onEdit();
			}
		});
		buttonBar.add(editButton);
	}

	@Override
	public void edit(String content)
	{
		int h = contentPanel.getElement().getAbsoluteBottom()
				- contentPanel.getElement().getAbsoluteTop() + 30;
		GWT.log("Height: " + h);
		flowPanel.clear();
		if(editor==null)
		{
			createEditor();
		}
		editor.setHtml(SafeHtmlUtils.fromTrustedString(content==null?"":content));
		flowPanel.add(editor);
		flowPanel.addStyleName(articleStyle.editMode());
		editor.setHeight(h + "px");
	}

	private void createEditor()
	{
		editor = htmlEditorProvider.get();
		editor.setStyleName(articleStyle.editBox());
		editor.setToolbarStyleName(articleStyle.buttonBar());
		Button saveButton = new Button("Vista");
		saveButton.setStyleName(pageStyle.saveButton());
		Button closeButton = new Button("Hætta við");
		closeButton.setStyleName(pageStyle.closeButton());
		editor.addToolbarWidget(saveButton);
		editor.addToolbarWidget(closeButton);
		saveButton.addClickHandler(new ClickHandler()
		{

			@Override
			public void onClick(ClickEvent event)
			{
				presenter.onSave(editor.getHtml());
			}
		});
		closeButton.addClickHandler(new ClickHandler()
		{

			@Override
			public void onClick(ClickEvent event)
			{
				presenter.onCancel();
			}
		});
	}

	@Override
	public void setCanEdit(boolean canEdit)
	{
		this.canEdit = canEdit;
	}

	@Override
	public void showLoadingIndicator()
	{
		flowPanel.clear();
		flowPanel.add(loadingIndcator);
	}

}
